// /*
//  * @Author: WanYiMing
//  * @Date:2024-07-05-14:20
//  * @Description:
//  */

using UnityEngine;

namespace Code.GameLogic.XianJian.Element.JianQiAction
{
    public class ActionLocking:AbsJianQiAction
    {
        private Transform _jianQiTransform;
        
        private Transform _targetTransform;

        private Rigidbody2D _rb;

        public override void ActionStart()
        {
            _jianQiTransform = JianQiActionData.JianQi.transform;
            _targetTransform  = JianQiActionData.Target.transform;
            _rb= JianQiActionData.Rigidbody2D;
            
        }

        public override bool ActionHolding()
        {
            Vector2 targetPosition = new Vector2(_targetTransform.transform.position.x, _targetTransform.transform.position.y);
            Vector2 currentPosition = new Vector2(_jianQiTransform.position.x, _jianQiTransform.position.y);

            if (Vector2.Distance(currentPosition, targetPosition) > .1f)
            {
                Vector2 direction = targetPosition - currentPosition;
                float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
                _jianQiTransform.rotation = Quaternion.Euler(new Vector3(0,0 , angle));
                Vector2 newPosition = Vector2.MoveTowards(currentPosition, targetPosition, JianQiActionData.Speed * Time.deltaTime);
                _rb.MovePosition(newPosition);
                return true;
            }

            return false;
        }

        public override void ActionExit()
        {
        }
    }
}